<template>
  <div class="app-container">
    <div v-if="!detailVisible">
      <div class="search">
        <el-input v-model="form.name" style="width: 200px;margin-right: 20px" clearable placeholder="请输入任务名称" />
        <el-select v-model="form.status" clearable filterable placeholder="请选择">
          <el-option
            v-for="sub in subStatus"
            :key="sub.value"
            :label="sub.label"
            :value="sub.value"
          />
        </el-select>
        <el-button type="primary" icon="el-icon-search" style="margin-left: 40px" @click="setResult">查询</el-button>
        <el-button type="primary" icon="el-icon-refresh" @click="reset">重置</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column prop="id" label="工单编号" width="100" />
        <el-table-column prop="name" label="任务名称" width="240">
          <template slot-scope="scope">
            <div>
              {{ scope.row.task && scope.row.task.name }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="收单时间">
          <template slot-scope="scope">
            <div>
              {{ getTimeFormate(scope.row.updated_at) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="填报人">
          <template slot-scope="scope">
            <div style="color: #ffad32">{{ scope.row.report_log && scope.row.report_log.operator && scope.row.report_log.operator.name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="填报时间">
          <template slot-scope="scope">
            <div>{{ scope.row.report_log && getTimeFormate(scope.row.report_log.updated_at) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="审核人">
          <template slot-scope="scope">
            <div>{{ scope.row.review1_log && scope.row.review1_log.operator.name }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="email" label="审核时间">
          <template slot-scope="scope">
            <div>{{ scope.row.review1_log && getTimeFormate(scope.row.review1_log.updated_at) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <div>
              {{ getStatus(scope.row.status) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleDetail(scope.row)">详情</el-button>
            <el-button v-if="scope.row.status == 1 || scope.row.status == 3" type="text" @click="handleSave(scope.row)">处理</el-button>
            <!-- <el-button v-if="scope.row.status == 2" type="text" @click="">撤回</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <Pagination :total="total" :limit="10" :background="false" :page="currentPage" @pagination="handleCurrentChange" @pageSize="handleSizeChange" />

    </div>
    <div v-if="detailVisible">
      <Detail v-if="detailVisible" :item="item" :detail-type="detailType" @goBack="goBack" @updateResult="updateResult" />
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import { getWorkFillList } from '@/api/workOrder'
import { getTask } from '@/api/table'
// import { getAccount, getDistrictList } from '@/api/authority'
import Detail from './detail.vue'
export default {
  components: {
    Detail
  },
  data() {
    return {
      tableData: [],
      form: {
        name: '',
        status: ''
      },
      item: {},
      detailVisible: false,
      listLoading: false,
      total: 0,
      currentPage: 1,
      pageSizes: 10,
      dedetailType: '',
      taskList: [],
      accountList: [],
      districtList: [],
      subStatus: [
        {
          value: '1',
          label: '待处理'
        }, {
          value: '2',
          label: '待初审'
        },
        {
          value: '3',
          label: '初审驳回'
        },
        {
          value: '4',
          label: '待复审'
        },
        {
          value: '5',
          label: '复审驳回'
        },
        {
          value: '6',
          label: '已完结'
        }
      ]
    }
  },
  created() {
    this.fetchData()
    // this.getTaskList()
    // this.getDicList()
    // this.getAccountList()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getWorkFillList({ ...this.form, page: this.currentPage, per_page: this.pageSizes }).then(response => {
        this.tableData = response.data
        this.total = response.pagination.total
        this.currentPage = response.pagination.current_page
        this.listLoading = false
      })
    },
    getTimeFormate(date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getTaskList() {
      getTask({ page: 1, per_page: 1000 }).then(response => {
        this.taskList = response.data
      })
    },
    handleCurrentChange(val) {
      this.currentPage = val.page
      this.fetchData()
    },
    handleSizeChange(val) {
      this.pageSizes = val.limit
      this.fetchData()
    },
    getTaskName(id) {
      let name = ''
      this.taskList.forEach(item => {
        if (item.id == id) {
          name = item.name
        }
      })
      return name
    },
    handleDetail(row) {
      // this.$router.push({
      //   path: 'detail',
      //   params: {
      //     item: row
      //   }
      // })
      // this.item = row
      this.detailType = 'detail'
      const routeUrl = this.$router.resolve({
        path: '/workOrder/detail',
        query: {
          id: row.id,
          type: 'detail'
        }
      })
      window.open(routeUrl.href, '_blank')
      // this.detailVisible = true
    },
    reset() {
      this.form = {
        name: '',
        status: ''
      }
      this.currentPage = 1
      this.fetchData()
    },
    setResult() {
      this.currentPage = 1
      this.fetchData()
    },
    updateResult() {
      this.detailVisible = false
      this.fetchData()
    },
    goBack() {
      this.detailVisible = false
    },
    getStatus(status) {
      let label = ''
      this.subStatus.forEach(item => {
        if (item.value == status) {
          label = item.label
        }
      })
      return label
    },
    handleSave(row) {
      // this.$router.push({
      //   name: 'Detail',
      //   query: {
      //     id: row.id
      //   }
      // })
      const routeUrl = this.$router.resolve({
        path: '/workOrder/detail',
        query: {
          id: row.id,
          type: 'save'
        }
      })
      window.open(routeUrl.href, '_blank')
      // this.item = row
      this.detailType = 'save'
      // this.detailVisible = true
    }
  }
}
</script>
<style lang="scss" scoped>
.search {
  margin-bottom: 20px;
}
</style>
